package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> pageRoles(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Role> roles = roleMapper.selectListRoles(name, label);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return new PageResult<>(pageInfo.getTotal(), roles);
    }

    @Override
    public void deleteRole(Long id) {
        roleMapper.deleteRoleById(id);
    }

    @Override
    public void saveRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insertRole(role);
    }

    @Override
    public Role getRoleById(Long id) {
        return roleMapper.selectRoleById(id);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRoleById(role);
    }

    @Override
    public List<Role> listAllRole() {
        return roleMapper.selectListRoles(null, null);
    }
}
